package com.jason.bookworm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jason.bookworm.dao.mapper.BookWareMapper;
import com.jason.bookworm.dao.pojo.Book;
import com.jason.bookworm.dao.pojo.BookClassify;

import com.jason.bookworm.service.BookWareService;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;


/**
 * 书籍相关方法
 *
 * @author YANGKUN
 */
@Service
public class BookWareServiceImpl extends ServiceImpl<BookWareMapper, Book> implements BookWareService {

    @Resource
    private BookWareMapper bookWareMapper;

    /**
     * 查询库中所有书籍
     *
     * @return
     */
    @Override
    public List<Book> getAllBooks() {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("b_status", "0");
        return bookWareMapper.selectList(queryWrapper);
    }

    /**
     * 按分类获得所有书籍
     *
     * @param bookClassify
     * @return
     */
    @Override
    public List<Book> getAllBooks(BookClassify bookClassify) {
        //分类对象获得分类编号
        int type = bookClassify.getId();
        //根据分类编号获得符合条件的书籍
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("b_type", type)
                .eq("b_status", "0");
        //返回
        return bookWareMapper.selectList(queryWrapper);
    }

    /**
     * 按分类获得所有书籍
     *
     * @param pages
     * @return
     */
    @Override
    public List<Book> getBooksByPages(int num, int pages) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("b_status", "0");
        //selectPage 第一个参数：分页查询条件 第二个参数：条件构造器
        //Page<>(当前页，每页多少条数据)
        Page<Book> books = bookWareMapper.selectPage(new Page<>(pages, num), queryWrapper);
        //Page类型转化成需要的List类型
        return books.getRecords();
    }

    /**
     * 按分类获得书籍
     *
     * @param pages
     * @param bookClassify
     * @return
     */
    @Override
    public List<Book> getBooksByPages(int num, int pages, BookClassify bookClassify) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("b_type", bookClassify.getId())
                .eq("b_status", "0");
        Page<Book> books = bookWareMapper.selectPage(new Page<>(pages, num), queryWrapper);
        //Page类型转化成需要的List类型
        return books.getRecords();
    }

    /**
     * 获得书籍的本数
     *
     * @return
     */
    @Override
    public Integer getNumOfBooks() {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("b_status", "0");
        return Math.toIntExact(bookWareMapper.selectCount(queryWrapper));
    }

    /**
     * 按分类获得书籍的本数
     *
     * @param bookClassify
     * @return
     */
    @Override
    public Integer getNumOfBooks(BookClassify bookClassify) {
        int type = bookClassify.getId();
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("b_status", "0")
                .eq("b_type", type);
        return Math.toIntExact(bookWareMapper.selectCount(queryWrapper));
    }

    /**
     * 获得书库的页数
     *
     * @return
     */
    @Override
    public Integer getPageOfBooks(int num) {
        return getNumOfBooks() / num + 1;
    }

    /**
     * 获得书库的页数
     *
     * @param bookClassify
     * @return
     */
    @Override
    public Integer getPageOfBooks(int num, BookClassify bookClassify) {
        return getNumOfBooks(bookClassify) / num + 1;
    }

    /**
     * 按书籍id获取这本书
     *
     * @param bookId
     * @return
     */
    @Override
    public Book getBookById(int bookId) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("b_id", bookId).eq("b_status", 0);
        return bookWareMapper.selectOne(queryWrapper);
    }

    /**
     * 模糊搜索(按书名)
     *
     * @param bookName
     * @return
     */
    @Override
    public List<Book> getBooksByBookName(String bookName) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        //图书状态正常，以书名模糊查询
        queryWrapper.eq("b_status", 0).like("b_name", bookName);
        return bookWareMapper.selectList(queryWrapper);
    }

    /**
     * 模糊搜索(按作者)
     *
     * @param author
     * @return
     */
    @Override
    public List<Book> getBooksByAuthor(String author) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        //图书状态正常，以作者模糊查询
        queryWrapper.eq("b_status", 0).like("b_author", author);
        return bookWareMapper.selectList(queryWrapper);
    }

    /**
     * 模糊搜索(按出版社)
     *
     * @param publishName
     * @return
     */
    @Override
    public List<Book> getBooksByPublishName(String publishName) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        //图书状态正常，以出版社模糊查询
        queryWrapper.eq("b_status", 0).like("b_press", publishName);
        return bookWareMapper.selectList(queryWrapper);
    }

    /**
     * 修改书籍信息
     *
     * @param book
     */
    @Override
    public void updateBook(Book book) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        //书籍状态正常&&确定要修改的书籍ID
        queryWrapper.eq("b_status", 0).eq("b_id", book.getId());
        //更新
        bookWareMapper.update(book, queryWrapper);
    }

    /**
     * 添加一本新书
     *
     * @param book
     */
    @Override
    public void addNewBook(Book book) {
        bookWareMapper.insert(book);
    }

    /**
     * 传入书籍对象删除这本书
     *
     * @param book
     */
    @Override
    public void deleteOneBook(Book book) {
        //书籍设置为逻辑删除
        book.setStatus("1");
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        //书籍状态正常&&确定要修改的书籍ID
        queryWrapper.eq("b_status", 0).eq("b_id", book.getId());
        //更新
        bookWareMapper.update(book, queryWrapper);
    }

    /**
     * @param reserveSet 预约清单
     * @return 书籍列表
     */
    @Override
    public List<Book> getBooksByBookIDSet(Set<String> reserveSet) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        for (String bookID : reserveSet) {
            queryWrapper.eq("b_id", bookID).or();
        }
        return bookWareMapper.selectList(queryWrapper);
    }

    /**
     * @param bookIDList 书籍id列表
     * @return 书籍列表
     */
    @Override
    public List<Book> getBooksByBookIDList(List<String> bookIDList) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        for (String bookID : bookIDList) {
            queryWrapper.eq("b_id", bookID).or();
        }
        return bookWareMapper.selectList(queryWrapper);
    }
}
